package org.dtrd.modules.food.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.food.entity.bean.RecipeCategoryDetail;
import org.dtrd.modules.food.entity.po.DtrdExtEmsYwRecipeCategory;
import org.dtrd.modules.food.entity.request.RecipeCategoryRequest;
import org.dtrd.modules.food.mapper.DtrdExtEmsYwRecipeCategoryMapper;
import org.dtrd.modules.food.service.IDtrdExtEmsYwRecipeCategoryService;
import org.dtrd.common.util.StringUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * <p>
 * 食谱分类 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-12-08
 */
@DS("multi-datasource1")
@Service
public class DtrdExtEmsYwRecipeCategoryServiceImpl extends ServiceImpl<DtrdExtEmsYwRecipeCategoryMapper, DtrdExtEmsYwRecipeCategory> implements IDtrdExtEmsYwRecipeCategoryService {

    private DtrdExtEmsYwRecipeCategoryMapper dtrdExtEmsYwRecipeCategoryMapper;

    @Override
    public List<RecipeCategoryDetail> getRecipeCategoryList(RecipeCategoryRequest request) {
        LambdaQueryWrapper<DtrdExtEmsYwRecipeCategory> queryWrapper = baseQueryWrapper();
        String recipeName = request.getRecipeName();
        if (StrUtil.isNotBlank(recipeName)) {
            queryWrapper.like(DtrdExtEmsYwRecipeCategory::getCategoryName, StringUtil.fuzzySearchWrapper(recipeName));
        }
        List<DtrdExtEmsYwRecipeCategory> poList = list(queryWrapper);
        List<RecipeCategoryDetail> dtoList = new ArrayList<>(poList.size());
        if (!poList.isEmpty()) {
            dtoList = poList.stream()
                .map(po -> new RecipeCategoryDetail().parseFromPo(po))
                .collect(Collectors.toList());
        }
        return dtoList;
    }

    @Override
    public RecipeCategoryDetail getRecipeCategory(Integer dataId) {
        DtrdExtEmsYwRecipeCategory dtrdExtEmsYwRecipeCategory = getById(dataId);
        return Optional.ofNullable(dtrdExtEmsYwRecipeCategory)
                        .map(po -> new RecipeCategoryDetail().parseFromPo(po))
                        .orElse(null);
    }

    @Override
    public boolean saveOrUpdateRecipeCategory(RecipeCategoryDetail dto) {
        DtrdExtEmsYwRecipeCategory dtrdExtEmsYwRecipeCategory = new DtrdExtEmsYwRecipeCategory();
        if(dto.getDataId() == null){
            dtrdExtEmsYwRecipeCategory = dtrdExtEmsYwRecipeCategory.init();
        }
        dtrdExtEmsYwRecipeCategory.parseFromDto(dto);
        return saveOrUpdate(dtrdExtEmsYwRecipeCategory);
    }

    @Override
    public boolean removeRecipeCategory(Integer dataId) {
        return removeById(dataId);
    }

    private LambdaQueryWrapper<DtrdExtEmsYwRecipeCategory> baseQueryWrapper() {
        return Wrappers.lambdaQuery(DtrdExtEmsYwRecipeCategory.class)
                        .eq(DtrdExtEmsYwRecipeCategory::getIsDel, 0);
    }

    private LambdaUpdateWrapper<DtrdExtEmsYwRecipeCategory> baseUpdateWrapper() {
        return Wrappers.lambdaUpdate(DtrdExtEmsYwRecipeCategory.class)
                        .eq(DtrdExtEmsYwRecipeCategory::getIsDel, 0);
    }
}
